var fromss;
var store1 = Ext.data.JsonStore({
	fields : []

});

var store2 = Ext.data.JsonStore({
	fields : []

});

Ext.onReady(function(){
Ext.BLANK_IMAGE_URL = "extjs/resources/images/default/s.gif";
	  Ext.QuickTips.init();
	windowst.show();
});

var toolbar=Ext.create('Ext.toolbar.Toolbar',{
	renderTo:document.body,
	width:550,
	items:[
	       {text:'新建',handler:function(){xinjian();}},
	       {text:'上一页',handler:function(){
	    	   if(datajson.toppage>datajson.currentpage){
	    		   alert("已是首页了。。。");
	    		   return;
	    	   }
	    	   queryxjzb(datajson.toppage);
	    	   grids.getStore().removeAll();
	       }},
	       {text:'下一页',handler:function(){
	    	   if(datajson.nextpage>datajson.pagesize-1){
	    		   alert("最后一页了。。。");
	    		   return;
	    	   }
	    	   queryxjzb(datajson.nextpage);
	    	   grids.getStore().removeAll();
	       }},
	       {text:'删除',handler:function(){dels();}},
	       {xtype:'tbspacer',width:220},
	       {text:'审核',handler:function(){shenhe();}},
	       {text:'反审核',handler:function(){fanshen();}},
	       ],
});
//审核
function shenhe(){ 
	alert(767676);
	var danhao=Ext.getCmp('billNo').getValue();
	alert(danhao);
	var shen=Ext.getCmp('shstatus').getValue();
	alert(shen);
	if(shen=="未审核"){
		Ext.Ajax.request({
			url:'http://localhost:8080/status',
			params:{billNo:danhao},
			method:"post",
			success:function(data){
				Ext.Msg.alert('审核成功!');
				queryxjzb(0);
			}
		});
	}else{
		Ext.Msg.alert('此单已审核!');
	}
	
}

//反审核
function fanshen(){
	alert(767676);
	var danhao=Ext.getCmp('billNo').getValue();
	var shen=Ext.getCmp('shstatus').getValue();
	if(shen=="已审核"){
		Ext.Ajax.request({
			url:'http://localhost:8080/fanstatus',
			params:{billNo:danhao},
			method:"post",
			success:function(data){
				Ext.Msg.alert('反审核成功!');
				queryxjzb(0);
			}
		});
	}else{
		Ext.Msg.alert('此单是未审核状态，不能反审核!');
	}
}

//删除
function dels(){
	alert(78676);
	var sc=Ext.getCmp('billNo').getValue();
	alert(sc);
	Ext.Ajax.request({
		url:'http://localhost:8080/ljsc',
		params:{billNo:sc},
		// 请求时使用的默认的http方法
		method : "post",
		success:function(e, response){
			Ext.Msg.alert("提示","删除成功!");
			grids.getStore().removeAll();
			queryxjzb(0);
		}
	});  
}

/*function dels(){
	var sc=Ext.getCmp('billNo').getValue();
	var records=grids.getSelectionModel().getSelection();
	if(records.length<=0){
		Ext.Msg.show({
			title:'提示',
			msg:'请选择要删除的行',
			buttons:Ext.MessageBox.Ok,
			icon:Ext.MessageBox.INFO
		});
		return;
	}
	if(records&&records!=''){
		Ext.Msg.confirm("操作提示","确定要删除选中行数据吗？",function(btn){
			if(btn=='yes'){
				grids.getSelectionModel().deselectAll(true);
				Ext.Object.each(records,function(key,value,obj){
					grids.getStore().remove(obj);
				});
			}
		});
	}
}*/

//新建
function xinjian(){
	Ext.getCmp('fromss').form.reset();
	grids.getStore().removeAll();
	zddanhao();
}

//自动生成单号
function zddanhao(){
	Ext.Ajax.request({
		url:'http://localhost:8080/querybillno',
		//headers:{"Content-Type":"application/json"},
		params:{billDate:Ext.getCmp('billDate').getValue()},
		// 请求时使用的默认的http方法
		method : "post",
		success:function(e, response){
			Ext.getCmp('billNo').setValue(e.responseText);
			Ext.getCmp('billDate').setValue(Ext.Date.add(new Date()),"Y-m-d");
		}
	});
}



var fielmenu=new Ext.menu.Menu({
	items:[{text:'历史交易查询',handler:function(){
		
		
		var gridss=Ext.create('Ext.grid.Panel',{
			columns:[
				{header:'(单别)',xtype:'rownumberer',dataIndex:'SerNo',width:50,sortable:false},
				{header:'(日期)',dataIndex:'ProdID',width:100,},
				{header:'(单号)',dataIndex:'PrdClassName',width:120,},
				{ header: '(币别)', dataIndex: 'bianma' },
				{ header: '(汇率)', dataIndex: 'lianxi' },
				{ header: '(物料编号)', dataIndex: 'lianxi'},
				{ header: '(物料名称)', dataIndex: 'lianxi' },
				{ header: '(规格型号)', dataIndex: 'lianxi' },
				{ header: '(数量)', dataIndex: 'lianxi' },
				{ header: '(单位)', dataIndex: 'lianxi' },
				{ header: '(折扣前单价)', dataIndex: 'lianxi' },
				{ header: '(折数%)', dataIndex: 'lianxi' },
				{ header: '(单价)', dataIndex: 'lianxi' },
				{ header: '(单价是否含税)', dataIndex: 'lianxi' },
				{ header: '(金额)', dataIndex: 'lianxi' },
				{ header: '(备注)', dataIndex: 'lianxi' },
			],
			height:260,
			width:1700,
			autoScroll:false,
			store : store2,
		});
		
		//头部form表单
		var formss=Ext.create('Ext.form.Panel', {
    	bodyPadding: 5,
		height:'100%',
		layout:"column",
		baseCls:'x-plain',
		items:[
		{
		style:'margin-left:20px;',
		xtype: 'textfield',
        name: 'Customer',
        fieldLabel: '供应商',
		width:250,
        labelWidth: 80,
        anchor: '100%',
		},{
		style:'margin-left:140px;',
		xtype: 'button',
        name: 'Customer',
        text: '资料输出',
		width:70,
        labelWidth: 80,
        anchor: '100%',
		},{
		style:'margin-left:10px;',
		xtype: 'button',
        name: 'Customer',
        text: '取回',
		width:70,
        labelWidth: 80,
        anchor: '100%',
		},gridss]
    });
		
		
		var windowstes=new Ext.Window({
			width:620,
			height:350,
			title:"历史交易查询",
			closable:true,
			resizable:false, //设置是否可以改变大小
			draggable:true,
			anchor: '100%',
 			items:[formss],
		}).show();
		
		gridss.getStore().removeAll();
		var customerName =Ext.getCmp('customerName').getValue();
		Ext.getCmp('customerName').setValue(customerName);
		Ext.Ajax.request({
			url : 'http://localhost:8080/lsjycs',
			params : {
				"customerName" : customerName,
			},
			method : "post",
			success : function(data) {

				//这里的response.responseText就是后台返回的数据
				//eval，将字符串转换为json对象
				
				var temp = JSON.parse(data.responseText);

				Ext.each(temp, function(i, e) {
					Ext.getCmp("customerName").setValue(
							temp[e].customerName);
					gridss.getStore().add(temp[e]);

				});

				//调用json对象的属性
			}
		});
		}}]
});
var fielmenu2=new Ext.menu.Menu({
	items:[{text:'采购请购单转入',handler:function(){
		zrqg();
	}},{text:'销售订单转入',handler:function(){
		zrdd();
	}}]
});

var fielmenu3=new Ext.menu.Menu({
	items:[
	       {text:'批次变更单价'},
	       {text : '保存',handler:function(){add();}},
	       {text : '修改',handler:function(){upda();}},
		  ]
});

//修改
function upda(){
	alert(78676);
	var data1 = forms.getForm().getValues();
	var data2 = formsg.getForm().getValues();
	var data3=Ext.pluck(grids.getStore().data.items,"data");
	Ext.each(data3,function(item){
		item.billNo=data1.billNo;
	});
	var date=Ext.apply(data1,data2);
	date.inquiryxb=data3;
	var date={
			billNo:Ext.getCmp('billNo').getValue(),
			customerName:Ext.getCmp('customerName').getValue(),
			addressId:Ext.getCmp('addressId').getValue(),
			custAdderss:Ext.getCmp('custAdderss').getValue(),
			validDate:Ext.getCmp('validDate').getValue(),
			priceOfTax:Ext.getCmp('priceOfTax').getValue(),
			billDate:Ext.getCmp('billDate').getValue(),
			currencyName:Ext.getCmp('currencyName').getValue(),
			exchRate:Ext.getCmp('exchRate').getValue(),
			outAddress:Ext.getCmp('outAddress').getValue(),
			salesName:Ext.getCmp('salesName').getValue(),
			departName:Ext.getCmp('departName').getValue(),
			maker:Ext.getCmp('maker').getValue(),
			permitter:Ext.getCmp('permitter').getValue(),
			shstatus:'未审核',
			isdelete:0,
			inquiryxb:Ext.pluck(grids.getStore().data.items,"data"),
	};
	Ext.Ajax.request({
		url:'http://localhost:8080/updatequi',
		headers:{"Content-Type":"application/json"},
		params:JSON.stringify(date),
		// 请求时使用的默认的http方法
		method : "post",
		success:function(date){
			alert(JOSN.stringify(date));
		}
	});
}

var grids=Ext.create('Ext.grid.Panel',{
		plugins: [
    				Ext.create('Ext.grid.plugin.CellEditing', {
        				clicksToEdit: 1
    				})
				],
columns: [
			 {header: '（栏号）',
                    		xtype: 'rownumberer',
                    		dataIndex: 'serNo', 
                    		width: 50,
							sortable: false
    					},
				
    		{ header: '物料编号',  dataIndex: 'prodId', width: 100,editor: { xtype:'textfield',
			 	listeners:{
					focus:function(grid, e, eOpts){
							windows2.show();
							}
						
				}
			 }},
   			 	 { header: '物料名称', dataIndex: 'prdClassName',width: 120,editor:"textfield"},
   				 { header: '规格型号', dataIndex: 'prodSize',editor:"textfield" },
				 { header: '单位名称', dataIndex: 'sunit',editor:"textfield" },
				 { header: '数量', dataIndex: 'sQuantity',editor:"textfield"},
				 { header: '折扣前单价', dataIndex: 'oldPrice',editor:"textfield" },
				 { header: '折数(%)', dataIndex: 'discount',editor:"textfield" },
				 { header: '单价', dataIndex: 'sPrice',editor:"textfield" },
				 { header: '金额', dataIndex: 'amount',editor:"textfield" },
				 { header: '税率', dataIndex: 'taxRate',editor:"textfield" },
				 { header: '税额', dataIndex: 'taxAmt',editor:"textfield" },
				 { header: '含税金额', dataIndex: 'vfTotal',editor:"textfield" },
				 { header: '赠品', dataIndex: 'isGift',editor:"textfield"},
				 { header: '分录备注', dataIndex: 'memo',editor:"textfield"},
				 { header: '来源单别', dataIndex: 'tranType',editor:"textfield"},
				 { header: '来源单号', dataIndex: 'fromNo',editor:"textfield"}
					 ],
		 height: 130,
		 width:1971,
		 autoScroll:false,
		 id:'grid1',
		 store : store1,
})

var filterPanel = Ext.create('Ext.panel.Panel', {
	bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
	width: 1985,
	height:170,
	items:[grids],
	bbar:[{xtype:"label",text:'总计:',width:90},
	{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 40 0 30',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
});

var  tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true,
	bodyPadding: 5,
	items:[{title:'内容',autoScroll:true,items:[filterPanel]},{title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}],
});


var formsg=Ext.create('Ext.form.Panel', {
	bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	
	items:[{
		id:'outAddress', 
	xtype: 'textfield',
    name: 'SalesMan',
	bodyPadding: 5,
    fieldLabel: '送货地址',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
			id:'maker',
	xtype: 'textfield',
    name: 'Marker',
    fieldLabel: '制单人员',
	style:'margin-left:19px',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
			id:'salesName',
	style:"color:blue;",
	 xtype: 'textfield',
    name: 'ProdDepart',
    fieldLabel: '采购人员',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
			id:'permitter',
	xtype: 'textfield',
    name: 'Permitter',
    fieldLabel: '复核人员',
	style:'margin-left:19px',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
			id:'departName',
	style:"color:blue;",
	 xtype: 'textfield',
    name: 'ProdProject',
    fieldLabel: '所属部门',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		}],
});


var forms=Ext.create('Ext.form.Panel', {
	bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	id:"fromss",
	items:[
	{
	id:"shstatus",
	style:'margin-left:9px;color:blue',
    xtype: 'textfield',
    name: 'shstatus',
    hidden:true,
    hiddenLaber:true,
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
	id:"customerName",
	style:'margin-left:9px;color:blue',
    xtype: 'textfield',
    name: 'customerName',
    fieldLabel: '供应商',
	width:250,
    labelWidth: 80,
    anchor: '100%',
		},{
	style:'margin-left:20px;color:blue',
	 xtype: 'datefield',
	 id:'billDate',
    name: 'billDate',
    fieldLabel: '单据日期',
    format:'y-m-d',
    //altFormats: "Y/m/d|Ymd",
    //value:new Date(),
	width:250,
    labelWidth: 70,
    anchor: '100%',
		},{
	id:"addressId",
	style:'margin-left:9px;',
    xtype: 'textfield',
    name: 'addressId',
    fieldLabel: '供应商地址',
	width:150,
    labelWidth: 80,
    anchor: '100%',
		},{
			id:'custAdderss', 
	xtype: 'textfield',
    name: 'custAdderss',
	width:99,
    labelWidth: 70,
	margin:"0 1",
    anchor: '100%',
		},{
			id:'billNo',
	style:'margin-left:9px',
	 xtype: 'textfield',
    name: 'billNo',
	style:"color:blue;margin-left:20px",
    fieldLabel: '单据号码',
	width:250,
    labelWidth: 70,
    anchor: '100%',
		},{
		id:'validDate',
		style:'margin-left:9px;',
		xtype: 'datefield',
	    name: 'validDate',
	    fieldLabel: '有效日期',
	    format:'y-m-d',
	    width:250,
	    labelWidth:80,
	    anchor: '100%',
				},{
					id:'currencyName',    
					style:'margin-left:20px;',
			        fieldLabel: '币别',
					xtype:'combo',
			        name: 'currencyName',
			        displayField: 'name',
			        labelWidth:70,
					width:250,
					valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
					fields: ['abbr', 'name'],
					data : [
			    			{"abbr":"人民币", "name":"人民币"},
			    			{"abbr":"欧元", "name":"欧元"},
			    			{"abbr":"英镑", "name":"英镑"},
			    			{"abbr":"美元", "name":"美元"}
						]
						}),
							value: '人民币',
			    			typeAhead: true,
			    			mode: 'local',
			    			triggerAction: 'all',
			    			selectOnFocus:true,
			    			
			         	},{
		id:'priceOfTax',    
		style:'margin-left:9px;',
        fieldLabel: '单价是否含税',
		xtype:'combo',
        name: 'IncludeRate',
        displayField: 'name',
        labelWidth:80,
		width:250,
		valueField: 'abbr',
		store: Ext.create('Ext.data.Store', {
		fields: ['abbr', 'name'],
		data : [
    			{"abbr":"含税", "name":"含税"},
    			{"abbr":"未税", "name":"未税"}
						
			]
			}),
				value: '含税',
    			typeAhead: true,
    			mode: 'local',
    			triggerAction: 'all',
    			selectOnFocus:true,
    			
         	},{
         		id:'exchRate',
         		style:'margin-left:20px',
				 xtype: 'textfield', //多行文本域
			    name: 'exchRate',
			    fieldLabel: '汇率',
			    labelWidth: 70,
				width:250,
			    anchor: '100%',
			},tableds,formsg]
});

//分页查询
var datajson;
queryxjzb(0);
function queryxjzb(id){
	//alert(id+"123132")
	Ext.Ajax.request({
		 //被用来向服务器发起请求默认的url  
  		  url : "http://localhost:8080/findAllz",  
  		//请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
 		  params :{currentpage:id},  
 	    //请求时使用的默认的http方法  
 		  method : "post",
 	   //请求成功时回调函数
 		  success:function(response){
 			  var temp=JSON.parse(response.responseText);
 			  datajson=temp;
 			  var temp1=Ext.JSON.decode(response.responseText,true);
 		    Ext.getCmp('shstatus').setValue(datajson.pageobj[0].shstatus);
			Ext.getCmp('billNo').setValue(datajson.pageobj[0].billNo);
 			Ext.getCmp('customerName').setValue(datajson.pageobj[0].customerName);
 			Ext.getCmp('addressId').setValue(datajson.pageobj[0].addressId);
 			Ext.getCmp('custAdderss').setValue(datajson.pageobj[0].custAdderss);
 			Ext.getCmp('validDate').setValue(datajson.pageobj[0].validDate);
 			Ext.getCmp('priceOfTax').setValue(datajson.pageobj[0].priceOfTax);
 			Ext.getCmp('billDate').setValue(datajson.pageobj[0].billDate);
 			Ext.getCmp('currencyName').setValue(datajson.pageobj[0].currencyName);
 			Ext.getCmp('exchRate').setValue(datajson.pageobj[0].exchRate);
 			Ext.getCmp('outAddress').setValue(datajson.pageobj[0].outAddress);
			Ext.getCmp('salesName').setValue(datajson.pageobj[0].salesName);
 			Ext.getCmp('departName').setValue(datajson.pageobj[0].departName);
 			Ext.getCmp('maker').setValue(datajson.pageobj[0].maker);
 			Ext.getCmp('permitter').setValue(datajson.pageobj[0].permitter);
 			grids.getStore().add(Ext.JSON.decode(response.responseText).pageobj[0].inquiryxb);
 		  },
	});
}

//新增
function add(){
	alert(123);
	var date={
			billNo:Ext.getCmp('billNo').getValue(),
			customerName:Ext.getCmp('customerName').getValue(),
			addressId:Ext.getCmp('addressId').getValue(),
			custAdderss:Ext.getCmp('custAdderss').getValue(),
			validDate:Ext.getCmp('validDate').getValue(),
			priceOfTax:Ext.getCmp('priceOfTax').getValue(),
			billDate:Ext.getCmp('billDate').getValue(),
			currencyName:Ext.getCmp('currencyName').getValue(),
			exchRate:Ext.getCmp('exchRate').getValue(),
			outAddress:Ext.getCmp('outAddress').getValue(),
			salesName:Ext.getCmp('salesName').getValue(),
			departName:Ext.getCmp('departName').getValue(),
			maker:Ext.getCmp('maker').getValue(),
			permitter:Ext.getCmp('permitter').getValue(),
			shstatus:'未审核',
			isdelete:0,
			inquiryxb:Ext.pluck(grids.getStore().data.items,"data"),
	};
	alert(JSON.stringify(date));
	Ext.Ajax.request({
		url:'http://localhost:8080/add',
		headers:{"Content-Type":"application/json"},
		params:JSON.stringify(date),
		success:function(response){
			Ext.MessageBox.alert("提示","新增成功",function(){});
		}
	});
}

//winform窗口
	var windowst=new  Ext.Window({
		width:560,
		height:530,
		title:"采购询价单",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		anchor: '100%',
		items:[toolbar,forms],
		bbar:[{xtype:"splitbutton",text:'查询',width:90,menu:fielmenu},
				{xtype:"splitbutton",text:'转单',width:90,menu:fielmenu2},
				{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu3}],
	});
	
	var windows2=new  Ext.Window({
		width:400,
		height:300,
		title:"物料选择",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		closeAction:"hide",
	});